﻿
using Furion.DatabaseAccessor;
using Furion.RemoteRequest;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Data;
using System.Linq;
using Timo.Model.Timo.dbo.Sys;
using Timo.Service;
using Timo.SqlSugar.DbContext;

namespace Timo.Api.Controllers;

[ApiDescriptionSettings(Tag = "测试控制器")]
[DynamicApiController]
public class TestService
{
    private readonly ISysUserService _userService; //仓储模式
    private readonly ISqlSugarClient db; //默认混合模式
    private readonly MultipleDBContext _multipleDBContext; //多库
    private readonly TimoDBContext _timoDB; //指定库

    public TestService(ISysUserService userService, ISqlSugarClient sqlSugarClient, MultipleDBContext multipleDBContext,TimoDBContext timoDB)
    {
        _userService = userService;
        db = sqlSugarClient;
        _multipleDBContext = multipleDBContext;
        _timoDB = timoDB;
    }
    [HttpPost]
    public string myVal()
    {
        return $"Hello {nameof(Furion)}";
    }

    [HttpGet]
    public async Task<List<SysUser>> Index()
    {
        //if (App.HostEnvironment.IsDevelopment())
        //{
        //    db.CodeFirst.InitTables("Timo.Model");
        //}
        //var webRootPath = App.HostEnvironment.ContentRootPath;
        //db.DbFirst.IsCreateAttribute().CreateClassFile(webRootPath, "Models");
        //仓储模式
        var res = await _userService.List(); 

        var a = await db.AsTenant().QueryableWithAttr<SysUser>().Where(t => t.Id >= 2).ToListAsync();
        return a;
    }

    [HttpGet]
    public async Task<List<SysUser>> SingleDBTest()
    {
        ///单库操作
        var testSql = await _timoDB.SugarClient.Ado.GetDataTableAsync("select * from SysUser");
        var user = await _timoDB.SugarClient.Ado.SqlQueryAsync<SysUser>("select * from SysUser");
        var us = await _timoDB.SugarClient.Queryable<SysUser>().Where(t=>t.Id>=2).ToListAsync();

        //指定库操作
        var tdb = _multipleDBContext.GetDbContext("Timo");
        var ts =await tdb.Queryable<SysUser>().FirstAsync(t => t.Id == 3);
        //混合模式。QueryableWithAttr，model必须指定库
        var a = await db.AsTenant().QueryableWithAttr<SysUser>().Where(t => t.Id >= 2).ToListAsync();
        return us;
    }

}
